package com.example.run_game;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.view.View;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList; 
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


public class AchievementCentre extends Activity {
	
	// All static variables
    // XML node keys
    static final String KEY_ACHIEVEMENT = "achievement"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_DESCRIPTION = "description";
    static final String KEY_DATE = "date";
    static final String KEY_THUMB_PATH = "thumb_path";
 
    ListView list;
    LazyAdapter adapter;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_achievement_centre);
          
        ArrayList<HashMap<String, String>> achievementsList = new ArrayList<HashMap<String, String>>();
 
        XMLParser parser = new XMLParser();
        //String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        //xml = parser.getXmlFromFile();
        String xml = "";
        InputStream is;
        try
		{
        	AssetManager assetManager = getAssets();
			is = assetManager.open("achievements_list.xml");
			xml = readInputStreamAsString(is);
		} catch (IOException e1)
		{
			e1.printStackTrace();
		}
        Document doc = parser.getDomElement(xml); // getting DOM element
 
        NodeList nl = doc.getElementsByTagName(KEY_ACHIEVEMENT);
        // looping through all song nodes &lt;song&gt;
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key =&gt; value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
            map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
            map.put(KEY_THUMB_PATH, parser.getValue(e, KEY_THUMB_PATH));
 
            // adding HashList to ArrayList
            achievementsList.add(map);
        }
 
        list=(ListView)findViewById(R.id.achievement_list);
 
        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, achievementsList,this);
        list.setAdapter(adapter);
 
        // Click event for single list row
       /* list.setOnItemClickListener(new OnItemClickListener() {
 
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
 
            }
        });*/
    }
    
    public static String readInputStreamAsString(InputStream in) 
	    throws IOException {

	    BufferedInputStream bis = new BufferedInputStream(in);
	    ByteArrayOutputStream buf = new ByteArrayOutputStream();
	    int result = bis.read();
	    while(result != -1) {
	      byte b = (byte)result;
	      buf.write(b);
	      result = bis.read();
	    }        
	    return buf.toString();
	}
}